# Contributor: Oleg Titov <oleg.titov@gmail.com>
# Maintainer: Oleg Titov <oleg.titov@gmail.com>
pkgname=k3s
_pkgver=1.24.2+k3s2
pkgver=${_pkgver/+k3s/.}
pkgrel=0
pkgdesc="Lightweight Kubernetes. 5 less than k8s"
url="https://k3s.io"
arch="all"
license="Apache-2.0"
options="!check" # No test suite from upstream
depends="
	cni-plugins
	cni-plugin-flannel
	conntrack-tools
	containerd
	coreutils
	dbus
	findutils
	ipset
	iptables
	"
makedepends="
	bash
	go
	linux-headers
	sqlite-dev
	sqlite-static
	yq
	zlib-dev
	zlib-static
	btrfs-progs-dev
	btrfs-progs-static
	libseccomp-dev
	libseccomp-static
	"
install="$pkgname.post-install $pkgname.pre-deinstall"
subpackages="$pkgname-doc $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/k3s-io/k3s/archive/v$_pkgver.tar.gz
	k3s.confd
	k3s.initd
	k3s.logrotate
	k3s.modules-load
	"
builddir="$srcdir/src/github.com/k3s-io/$pkgname"

# secfixes:
#   1.21.3.1-r0:
#     - CVE-2021-32001
#   1.21.1.1-r0:
#     - CVE-2021-30465
#   1.20.6.1-r0:
#     - CVE-2021-25735
#   1.20.5.1-r0:
#     - CVE-2021-21334
#   1.19.4.2-r0:
#     - CVE-2020-15257
#   1.18.6.1-r0:
#     - CVE-2020-8557
#     - CVE-2020-8559
#   1.18.3.1-r0:
#     - CVE-2020-10749
#     - CVE-2020-8555
#   0.8.1-r0:
#     - CVE-2019-9512
#     - CVE-2019-9514
#   0.8.0-r0:
#     - CVE-2019-11247
#     - CVE-2019-11249

export GOFLAGS="$GOFLAGS -modcacherw"

prepare() {
	export GOPATH="$srcdir"

	mkdir -p "$(dirname $builddir)"
	mv "$srcdir"/$pkgname-${_pkgver/+/-} "$builddir"/

	default_prepare
}

build() {
	msg2 "Prepare GOPATH and GOBIN..."
	export GOPATH="$srcdir"
	export GOBIN="$GOPATH/bin"

	msg2 "Building k3s..."

	mkdir -p build/data && ./scripts/download && go generate

	TAGS="apparmor seccomp netcgo osusergo providerless static_build libsqlite3"
	STATIC_SQLITE="-extldflags '-static -lm -ldl -lz -lpthread'"
	VERSIONFLAGS="
	-X github.com/k3s-io/k3s/pkg/version.Version=v$_pkgver
	-X github.com/k3s-io/k3s/pkg/version.GitCommit=AlpineLinux
	"
	GOLDFLAGS="-w -s"

	go build -o k3s -tags "$TAGS" -ldflags "$VERSIONFLAGS $GOLDFLAGS $STATIC_SQLITE" ./cmd/server/main.go
}

package() {
	install -m755 -D k3s \
		"$pkgdir"/usr/bin/k3s

	install -m644 -D -t "$pkgdir"/usr/share/doc/$pkgname README.md

	install -m755 -D "$srcdir"/k3s.initd "$pkgdir"/etc/init.d/k3s
	install -m644 -D "$srcdir"/k3s.confd "$pkgdir"/etc/conf.d/k3s
	install -m644 -D "$srcdir"/k3s.logrotate "$pkgdir"/etc/logrotate.d/k3s
	install -m644 -D "$srcdir"/k3s.modules-load "$pkgdir"/usr/lib/modules-load.d/k3s.conf
}

sha512sums="
22d455a9edbcca832c22e56c92994d95913b97d48f23757d6dae35e94a734be9ba39471f2f00b06e6b443727dc9824d15b0086398ada5dae977378ed5ca39029  k3s-1.24.2.2.tar.gz
f03221efceb4ce2305c41c4c9e6d02ee5b799ed0cdfb1fc5018f8696e4d05575ae63b7c87596d765c5aa76c4a3bacf7c205e3eb61465e26886081a5d0da013ea  k3s.confd
1015ee6ce5c69595df3150d7bbdfe528cf20305dac299831faa9cce00a454daf5548e78b1db79dcb8da300edc54553dfda0b95aed5e7bee27c1c726aef640350  k3s.initd
018a5e9b417a937c17f0a4a9e08eed434f06186207626ad038aec22ee667aba4cefa6e9e2a222e2c430d2cbb88c8663648f5bab0e76926a0edd13b8bdfd2673a  k3s.logrotate
85ee1310cb36c85c42b4068a9549a3ef72b856cd61b2c1036c3e871ef43a69ed80b43599ad94ce5b069ddd823e730596bb3d3875d4ba8cd77c4cc1985335ffff  k3s.modules-load
"
